% Z = POLYMULT(X,Y) Multiply the two polynomials X and Y
%
%   Inputs:
%
%       x,y     Polynomial multiplicands
%
%   Outputs:
%
%       z       The product of x and y

function z = polyMult(x,y)

    % if either polynomial is zero, return zero
    if polyIsZero(x) || polyIsZero(y)
        z = -1;
    else
        % Determine which polynomial is shorter
        if length(x) > length(y)
            shortPoly = y;
            longPoly = x;
        else
            shortPoly = x;
            longPoly = y;
        end

        % Initialize output polynomial vector
        z = -1 * ones(1,length(x) + length(y) - 1);

        % Multiply the polynomials
        for i = 1 : length(shortPoly)
            %Pad by the power of x
            coef = -1 * ones(1,i-1);
            %Multiply the coefficients
            if ~isZero(shortPoly(i))
                for j = 1 : length(longPoly)            
                    coef = [coef elementMult(shortPoly(i),longPoly(j))];
                end
            end

            %Add this polynomial to the output
            z = polyAdd(z,coef);
       end
    end

    %Get rid of any leading zeros
    z = polyTrimZeros(z);
end
